#include "functions.h"

void set_parameters( int number_of_arguments, char **arguments ){

  int i;

  set_initial_parameters();
   
  for( i = 1; i < number_of_arguments ; i++ ){
    if( strcmp( arguments[ i ], "--individuals" ) == 0 ){
      each_individuals = atoi( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--layers" ) == 0 ){
      number_of_layers = atoi( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--alpha" ) == 0 ){
      alpha = atof( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--K" ) == 0 ){
      K = atof( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--function" ) == 0 ){
      function_name = arguments[ ++i ];
      populate_upper_and_lower_limits();
    }

    else if( strcmp( arguments[ i ], "--dimensions" ) == 0 ){
      number_of_dimensions = atoi( arguments[ ++i ] );
    } 
    
    else if( strcmp( arguments[ i ], "--evaluations" ) == 0 ){
      max_evaluations = atoi( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--seed" ) == 0 ){
      seed = atoi( arguments[ ++i ] );
      srand( seed );
    }

    else if( strcmp( arguments[ i ], "--beta1" ) == 0 ){
      beta1 = atof( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--betaL" ) == 0 ){
      betaL = atof( arguments[ ++i ] );
    }

    else if( strcmp( arguments[ i ], "--without_msg" ) == 0 ) {	
      msg_type = 0;
    }

    else if( strcmp( arguments[ i ], "--gnuplot" ) == 0 ){
      gnuplot_type = 1; // write file for gnuplot
    }

    else {
      print_arguments_description();		
    }

  } // end for

  total_population_size = number_of_layers * each_individuals;

// print initial parameters
  printf( "\n" );
  printf( "Function\t\t: %s\n", function_name );
  printf( "Number of dimensions\t: %d\n", number_of_dimensions );
  printf( "Upper bound\t\t: %.2f\n", upper_limit );
  printf( "Lower bound\t\t: %.2f\n", lower_limit );
  printf( "Max evaluations\t\t: %d\n", max_evaluations );
  printf( "Seed of randam number\t: %d\n", seed );
  printf( "Population Size\t\t: %d\n", total_population_size );
  printf( "Number of Layers\t: %d\n", number_of_layers );
  printf( "Create Individuals\t: %d\n", each_individuals );
  printf( "Elite Rate\t\t: %f\n", K );
  printf( "Beta1\t\t\t: %.5f\n", beta1 );
  printf( "BetaL\t\t\t: %.5f\n", betaL );
  printf( "Alpha\t\t\t: %.5f\n", alpha );

  if( gnuplot_type == 0 )
    printf( "Gnuplot?\t\t: OFF\n" );
  else
    printf( "Gnuplot?\t\t: ON\n" );


  // Parameter Check!! 
  if( gnuplot_type == 1 && number_of_dimensions != 2 ){
    printf( "Bad Parameters!! If you use \"--gnuplot\", you must use \"--number_of_dimensions 2\".\n");
    exit(1);
  }

  printf( "\n" );
}

void set_initial_parameters(){

  each_individuals = 16;
  number_of_layers = 4;

  function_name = "Rastrigin-1.0";
  number_of_dimensions = 20;
  upper_limit = 5.12;
  lower_limit = -5.12;

  seed = 0;
  srand( seed );

  alpha = 0.01;

  msg_type = 1; // 1: ON 0: OFF
  gnuplot_type = 0; // 1: ON 0: OFF
  
  max_evaluations = 1e+7;
  K = 0.5;
  beta1 = 0.001;
  betaL = 0.05;

  number_of_evaluations = 0;

  termination_eps = 1e-7;

  dirname="gnuplot";
}

void populate_upper_and_lower_limits(){
  if( strcmp( function_name, "Rastrigin-1.0" ) == 0 ){
    upper_limit = 5.12;
    lower_limit = -5.12;
	
  } else if( strcmp( function_name, "RotatedRastrigin-1.0" ) == 0 ) {
    upper_limit = 5.12;
    lower_limit = -5.12;
  
  } else if( strcmp( function_name, "Ellipsoid" ) == 0 ) {
    upper_limit = 5.12;
    lower_limit = -5.12;

  } else if( strcmp( function_name, "k-tablet" ) == 0 ) {
    upper_limit = 5.12;
    lower_limit = -5.12;

  } else if( strcmp( function_name, "Sphere" ) == 0 ) {
    upper_limit = 5.12;
    lower_limit = -5.12;

  } else if( strcmp( function_name, "Rosenbrock-Star" ) == 0 ) {
    upper_limit = 2.048;
    lower_limit = -2.048;

  } else if( strcmp( function_name, "Rosenbrock-Chain" ) == 0 ) {
    upper_limit = 2.048;
    lower_limit = -2.048;
	
  } else if( strcmp( function_name, "Schwefel" ) == 0 ){
    upper_limit = 512.0;
    lower_limit = -512.0;
		
  } else if( strcmp( function_name, "Ridge" ) == 0 ){
    upper_limit = 64.0;
    lower_limit = -64.0;
		
  } else if( strcmp( function_name, "Griewank" ) == 0 ){
    upper_limit = 512.0;
    lower_limit = -512.0;
		
  } else if( strcmp( function_name, "Bohachevsky" ) == 0 ){
    upper_limit = 5.12;
    lower_limit = -5.12;

  } else if( strcmp( function_name, "Ackley" ) == 0 ){
    upper_limit = 32.768;
    lower_limit = -32.768;

  } else if( strcmp( function_name, "Schaffer" ) == 0 ){
    upper_limit = 100;
    lower_limit = -100;

  } else if( strcmp( function_name, "f1" ) == 0 ){
    upper_limit = 3.0;
    lower_limit = -3.0;
    termination_eps = 1e+7;

  } else if( strcmp( function_name, "f2" ) == 0 ){
    upper_limit = 3.0;
    lower_limit = -3.0;
    termination_eps = 1e+7;

  } else if( strcmp( function_name, "f3" ) == 0 ){
    upper_limit = 3.0;
    lower_limit = -3.0;
    termination_eps = 1e+7;

  } else if( strcmp( function_name, "f7" ) == 0 ){
    upper_limit = 30.0;
    lower_limit = -30.0;
    termination_eps = -1000;

  } else if( strcmp( function_name, "f8" ) == 0 ){
    upper_limit = 100;
    lower_limit = -100;

  } else {
    fprintf( stderr, "%s: invalid problem name\n", function_name );
    exit( 2 );
    
  }
}

void print_arguments_description(){
  fprintf( stderr, "argument \t\t: description ( default value )\n" );
  fprintf( stderr, "--individuals \t\t: create individuals on each layer ( %d )\n", each_individuals );
  fprintf( stderr, "--layers \t\t: number of layers ( %d )\n", number_of_layers );
  fprintf( stderr, "--function \t\t: function name ( %s )\n", function_name );
  fprintf( stderr, "          \t\t  [ Rastrigin-1.0, RotatedRastrigin, Rosenbrock-Star, Rosenbrock-Chain, Schwefel\n");
  fprintf( stderr, "          \t\t    Ridge, Griewank, Sphere, Ellipsoid, k-tablet, Bohachevsky, Ackley, Schaffer ]\n");
  fprintf( stderr, "          \t\t    f1, f2, f3, f7, f8 ]\n");
  fprintf( stderr, "--dimensions \t\t: number of dimensions ( %d )\n", number_of_dimensions );
  fprintf( stderr, "--evaluations \t\t: number of evaluations ( %d )\n", max_evaluations );
  fprintf( stderr, "--seed \t\t\t: integer value ( %d )\n", seed );
  fprintf( stderr, "--alpha \t\t: Learning Rate ( %.3f )\n", alpha );
  fprintf( stderr, "--without_msg \t\t: message is a minimum\n" );
  fprintf( stderr, "--gnuplot \t\t: Write file for Gnuplot\n" );
  fprintf( stderr, "--K \t\t\t: Elites Rate ( %f )\n", K );
  fprintf( stderr, "--beta1 \t\t: Beta1 ( %.5f )\n", beta1 );
  fprintf( stderr, "--betaL \t\t: BetaL ( %.5f )\n", betaL );
  
  exit( 2 );
}

